package com.hmall.search.listener;

import com.hmall.api.dto.ItemDTO;
import com.hmall.common.utils.BeanUtils;
import com.hmall.search.domain.po.ItemDoc;
import com.hmall.search.service.ISearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class Itemlistener {

    private final ISearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.add"),
            exchange = @Exchange(name = "item.direct"),
            key = "add"))
    public void addItem(ItemDTO item) throws IOException {
        ItemDoc itemDoc = BeanUtils.toBean(item, ItemDoc.class);
        searchService.addORupdate(itemDoc);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.update"),
            exchange = @Exchange(name = "item.direct",type = "direct"),
            key = "update"))
    public void updateItem(ItemDTO item) throws IOException {
        ItemDoc itemDoc = BeanUtils.toBean(item, ItemDoc.class);
        searchService.addORupdate(itemDoc);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.delete"),
            exchange = @Exchange(name = "item.direct",type = "direct"),
            key = "delete"))
    public void deleteItem(Long id) throws IOException {
        searchService.delete(id);
    }
}
